Imports WinXound_Net.gVar
Imports System.Runtime.InteropServices


Public Class FormExtraFontDialog


    Private FONT_NAME As String = "Courier New"
    Private FONT_SIZE As Int32 = 10

    Private Const DEFAULT_CHARSET As Integer = 1
    Private Const FIXED_PITCH As Integer = 1
    Private Const FF_DONTCARE As Integer = 0

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    Public Structure LOGFONT
        Public lfHeight As Integer
        Public lfWidth As Integer
        Public lfEscapement As Integer
        Public lfOrientation As Integer
        Public lfWeight As Integer
        Public lfItalic As Byte
        Public lfUnderline As Byte
        Public lfStrikeOut As Byte
        Public lfCharSet As Byte
        Public lfOutPrecision As Byte
        Public lfClipPrecision As Byte
        Public lfQuality As Byte
        Public lfPitchAndFamily As Byte
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
        Public lfFaceName As String
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure ENUMLOGFONTEX
        Public elfLogFont As LOGFONT
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=64)> _
        Public elfFullName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
        Public elfStyle As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
        Public elfScript As String
    End Structure


#Region "Remmed Code "
    ''<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    ''Public Structure NEWTEXTMETRIC
    ''    Public tmHeight As Integer
    ''    Public tmAscent As Integer
    ''    Public tmDescent As Integer
    ''    Public tmInternalLeading As Integer
    ''    Public tmExternalLeading As Integer
    ''    Public tmAveCharWidth As Integer
    ''    Public tmMaxCharWidth As Integer
    ''    Public tmWeight As Integer
    ''    Public tmOverhang As Integer
    ''    Public tmDigitizedAspectX As Integer
    ''    Public tmDigitizedAspectY As Integer
    ''    Public tmFirstChar As Byte
    ''    Public tmLastChar As Byte
    ''    Public tmDefaultChar As Byte
    ''    Public tmBreakChar As Byte
    ''    Public tmItalic As Byte
    ''    Public tmUnderlined As Byte
    ''    Public tmStruckOut As Byte
    ''    Public tmPitchAndFamily As Byte
    ''    Public tmCharSet As Byte
    ''    Public ntmFlags As Integer
    ''    Public ntmSizeEM As Integer
    ''    Public ntmCellHeight As Integer
    ''    Public ntmAveWidth As Integer
    ''End Structure

    ''<StructLayout(LayoutKind.Sequential)> _
    ''Public Structure FONTSIGNATURE
    ''    <MarshalAs(UnmanagedType.LPArray, SizeConst:=4)> _
    ''    Public fsUsb As Integer()
    ''    <MarshalAs(UnmanagedType.LPArray, SizeConst:=2)> _
    ''    Public fsCsb As Integer()
    ''End Structure

    ''<StructLayout(LayoutKind.Sequential)> _
    ''  Public Structure NEWTEXTMETRICEX
    ''    Public ntmTm As NEWTEXTMETRIC
    ''    Public ntmFontSig As FONTSIGNATURE
    ''End Structure
#End Region
 

    Private Declare Function EnumFontFamiliesEx Lib "gdi32" Alias "EnumFontFamiliesExA" _
            (ByVal hDC As IntPtr, _
             ByRef lpLogFont As LOGFONT, _
             ByVal lpEnumFontProc As EnumFontFamExProcDelegate, _
             ByVal lParam As Integer, _
             ByVal dw As Integer) As Integer


    Public Delegate Function EnumFontFamExProcDelegate _
            (ByVal lpelfe As IntPtr, _
             ByVal lpntme As IntPtr, _
             ByVal FontType As Int32, _
             ByVal lParam As Int32) As Int32


    Public Property FontName() As String
        Get
            Return FONT_NAME
        End Get
        Set(ByVal value As String)
            FONT_NAME = value
        End Set
    End Property

    Public Property FontSize() As Int32
        Get
            Return FONT_SIZE
        End Get
        Set(ByVal value As Int32)
            FONT_SIZE = value
        End Set
    End Property


    Private Sub FormExtraFontDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim logfont As LOGFONT = New LOGFONT()
        Dim g As Graphics = Me.CreateGraphics()
        Dim hdc As IntPtr = g.GetHdc()

        logfont.lfCharSet = DEFAULT_CHARSET
        logfont.lfPitchAndFamily = FF_DONTCARE ''''FIXED_PITCH Or FF_DONTCARE
        logfont.lfFaceName = String.Empty

        Try
            Dim CallBackProc As EnumFontFamExProcDelegate = New EnumFontFamExProcDelegate(AddressOf EnumFontFamExProc)
            EnumFontFamiliesEx(hdc, logfont, CallBackProc, 1, 0)
        Catch ex As Exception
            wxMessageError(ex.Message, "FormExtraFontDialog_Load - Error")
        End Try

        g.ReleaseHdc(hdc)

        Try

            If ListBoxName.Items.Contains(FONT_NAME) Then
                ListBoxName.SelectedItem = FONT_NAME
            Else
                ListBoxName.SelectedIndex = 0
            End If

            SizeBox.Value = FONT_SIZE
            SCI_EDIT_EXAMPLE.SetReadOnly(True)

        Catch ex As Exception
        End Try

    End Sub


    ' Callback function
    Private Function EnumFontFamExProc(ByVal lpelfe As IntPtr, ByVal lpntme As IntPtr, ByVal FontType As Integer, ByVal lParam As Integer) As Integer

        Try
            ' Get the structure from the pointer
            Dim elf As ENUMLOGFONTEX = CType(Marshal.PtrToStructure(lpelfe, GetType(ENUMLOGFONTEX)), ENUMLOGFONTEX)

            '' The two low-order bits specify the pitch of the font.
            ''Dim paf As Integer = elf.elfLogFont.lfPitchAndFamily '& 0x03 
            ''If paf = FIXED_PITCH Then
            ''    Console.WriteLine(elf.elfFullName + " - " + elf.elfScript)
            ''End If
            If Not ListBoxName.Items.Contains(elf.elfLogFont.lfFaceName) Then
                ListBoxName.Items.Add(elf.elfLogFont.lfFaceName)
            End If
        Catch ex As Exception
        End Try

        Return 1

    End Function



    Private Sub FormExtraFontDialog_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            ButtonExit.PerformClick()
        End If
    End Sub

    Private Sub ButtonOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOK.Click
        If ListBoxName.SelectedIndex > -1 And ListBoxName.SelectedItem <> "" Then
            FONT_NAME = ListBoxName.SelectedItem.ToString
        End If
        FONT_SIZE = SizeBox.Value
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub ButtonExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonExit.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub



    Private Sub ListBoxName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBoxName.SelectedIndexChanged
        Try
            RefreshText()
        Catch ex As Exception
        End Try
    End Sub


    Private Sub SizeBox_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SizeBox.ValueChanged
        Try
            RefreshText()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub RefreshText()
        Try
            Dim mText As String = "sr = 44100" & vbCrLf & _
                                  "kr = 4410" & vbCrLf & _
                                  "ksmps = 10" & vbCrLf & _
                                  "nchnls = 1" & vbCrLf & vbCrLf & _
                                  "     instr 1" & vbCrLf & _
                                  "a1   oscili 10000, 400, 1" & vbCrLf & _
                                  "     out a1" & vbCrLf & _
                                  "     endin"

            SCI_EDIT_EXAMPLE.StyleSetFont(STYLE_DEFAULT, ListBoxName.SelectedItem.ToString)
            SCI_EDIT_EXAMPLE.StyleSetSize(STYLE_DEFAULT, SizeBox.Value)
            SCI_EDIT_EXAMPLE.SetZoom(0)

            SCI_EDIT_EXAMPLE.SetText(mText)

        Catch ex As Exception
            ''''wxMessageError(ex.Message, "FormExtraFontDialog - RefreshText error")
        End Try


    End Sub


End Class